﻿//-------------------------------------------------------------------------------
// <copyright file="WebRequestResult.cs" company="TaskyMedia LLC">
//     Copyright (c) TaskyMedia LLC.
// </copyright>
//-------------------------------------------------------------------------------

/*
 * Copyright (c) 2010, TaskyMedia LLC
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without modification, 
 * are permitted provided that the following conditions are met:
 *   Redistributions of source code must retain the above copyright notice, this 
 *     list of conditions and the following disclaimer.
 *   Redistributions in binary form must reproduce the above copyright notice, this
 *     list of conditions and the following disclaimer in the documentation and/or
 *     other materials provided with the distribution.
 *   Neither the name of TaskyMedia LLC nor the names of its contributors may be used
 *   to endorse or promote products derived from this software without specific prior
 *   written permission.
 *   
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
 * IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
 * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
 * OF SUCH DAMAGE. 
 */

namespace FBGraphApi
{
    using System;
    using System.IO;
    using System.Net;
    using System.Text;

    /// <summary>
    /// Contains the metadata from a <see cref="T:System.Net.HttpWebResponse" /> and any data in the stream.
    /// </summary>
    internal class WebRequestResult : IDisposable
    {
        /// <summary>
        /// HTTP status code of the response.
        /// </summary>
        private HttpStatusCode _status;

        /// <summary>
        /// Data received in the HTTP response.
        /// </summary>
        private MemoryStream _responseDataStream;

        /// <summary>
        /// Initializes a new instance of the <see cref="WebRequestResult"/> class.
        /// </summary>
        /// <param name="statusCode">The status code of the response.</param>
        public WebRequestResult(HttpStatusCode statusCode)
        {
            this._status = statusCode;
            this._responseDataStream = new MemoryStream();
        }

        /// <summary>
        /// Gets the HTTP status code associated with the result.
        /// </summary>
        /// <value>The HTTP status code.</value>
        public HttpStatusCode Status
        {
            get
            {
                return this._status;
            }
        }

        /// <summary>
        /// Gets the data received from the server.
        /// </summary>
        /// <value>The data received from the server.</value>
        public string Data
        {
            get
            {
                this._responseDataStream.Position = 0;
                using (StreamReader sr = new StreamReader(this._responseDataStream))
                {
                    return sr.ReadToEnd();
                }
            }
        }

        /// <summary>
        /// Gets the raw data (byte array) received from the server.
        /// </summary>
        /// <returns>Returns a byte array containing the data received from the server.</returns>
        public byte[] GetRawData()
        {
            return this._responseDataStream.ToArray();
        }

        /// <summary>
        /// Writes a chunk of data to the result object.
        /// </summary>
        /// <param name="buffer">The buffer to write data from.</param>
        /// <param name="offset">The byte offset in the buffer at which to begin writing from.</param>
        /// <param name="count">The maximum number of bytes to write.</param>
        public void WriteChunk(byte[] buffer, int offset, int count)
        {
            this._responseDataStream.Write(buffer, offset, count);
        }

        /// <summary>
        /// Performs application-defined tasks associated with freeing, releasing,
        /// or resetting unmanaged resources.
        /// </summary>
        public void Dispose()
        {
            this._responseDataStream.Dispose();
        }
    }
}
